package cloud.health.framework.computing.gateway;

import cloud.health.framework.computing.core.GlobalProperties;
import cloud.health.framework.computing.gateway.domain.Gateway;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Objects;
import java.util.Optional;

@Slf4j
public class GatewayApplication {

    @SneakyThrows
    public static void main(String[] args) {
        Integer port = 7000;
        GlobalProperties.read();
        HashMap map = GlobalProperties.get("common", HashMap.class);
        if (Objects.nonNull(map) && Objects.nonNull(map.get("port"))) {
            port = Integer.valueOf(map.get("port").toString());
        }
        Optional<String> opt = Arrays.asList(args).stream()
                .filter(it -> it.startsWith("-D") && it.contains("common.port")).findFirst();
        if (opt.isPresent()) {
            log.info("opt = {}", opt.get());
            port = Integer.valueOf(opt.get().substring(opt.get().lastIndexOf("=") + 1));
        }
        Gateway gateway = new Gateway(port);
        gateway.run();

        for (; ; ) {
            Thread.sleep(10000L);
        }
    }

}
